
new Vue({
  // el: '#bmi',
  data: {
    msg: 'hello BMI',
    height: '',
    weight: '',
    bmiList: [],
    isShowResult: false,
    bmi: 23.3,
    status: ''
  },
  // 生命周期 钩子函数
  /*
  * 虚拟DOM
  * 双向数据绑定
  *
  * 数据驱动
  * 单页面应用
  *
  * # beforeCreate() {}
  * let div = document.createElement('div')
  * # created() {}
  * # beforeMount() {}
  * bmi.append(div)
  * # mounted() {}
  *
  * */
  beforeMount() {
    console.log('beforeMount')
  },
  mounted() {
    console.log('mounted')
  },
  beforeCreate() {
    console.log('beforeCreate')
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      const self = this
      Zepto.ajax({
        url: 'https://veihwwnelcwq.leanapp.cn/bmi',
        success(res) {
          // console.log(res.data)
          self.bmiList = res.data
          // console.log(self)
        }
      })
    },
    formatTime(time) {
      let date = new Date(time)
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
    },
    calcBMI() {
      const self = this
      const numReg = /^[0-9]*$/
      if (this.height && this.weight) {
        if (numReg.test(this.height) && numReg.test(this.weight)) {
          // console.log(this.height)
          // console.log(this.weight)
          Zepto.ajax({
            type: 'POST',
            url: 'https://veihwwnelcwq.leanapp.cn/bmi',
            data: {
              height: this.height,
              weight: this.weight
            },
            success(res) {
              const {status, bmi} = res.data
              self.status = status
              self.bmi = bmi
              self.isShowResult = true
              self.getList()
            }
          })
        } else {
          alert('身高体重必须为数字')
        }
      } else {
        alert('身高体重不能为空')
      }
    }
  }
}).$mount('#bmi')








